package com.jiyun.controller;

import com.jiyun.entity.UsianUser;
import com.jiyun.service.UsianUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/usian")
public class UsianUserController {

    @Autowired
    private UsianUserService usianUserService;

    //修改密码
    @RequestMapping("/updatepassword")
    public String updatepassword(){

        return "redirect:tologin";
    }

    //去修改密码
    @RequestMapping("/toeditpassword")
    public String toeditpassword(){
        return "editpassword";
    }

    //注册
    @RequestMapping("regist")
    public String regist(UsianUser user, HttpServletRequest request)  {

        String pwd = request.getParameter("repassword");
        if (user.getPassword().trim().equals(pwd.trim())){
            usianUserService.addUsianUser(user);
            return "redirect:tologin?sucess=3";

        }else {
            request.setAttribute("err","2");
            return "redirect:toregist?err=2";
        }

    }

    //去注册
    @RequestMapping("toregist")
    public String toregist(String sucess,String err,HttpServletRequest request){
        if ("2".equals(err)) {
            request.setAttribute("err","两次密码不一致");
        }
        return "regist";
    }

    @RequestMapping("login")
    public String login(UsianUser user, HttpServletRequest request){
        if (usianUserService.login(user)) {

            request.getSession().setAttribute("usian",user);

            return "redirect:/goods/findAll";
        }else{
            request.setAttribute("err","1");
            return "redirect:tologin?err=1";
        }

    }

    @RequestMapping("/tologin")
    public String tologin(String sucess,String err,HttpServletRequest request){
        if ("1".equals(err)){
            request.setAttribute("err","用户名或密码错误");
        }
        if ("3".equals(sucess)){
            request.setAttribute("err","注册成功，请登录");
        }
        return "login";
    }
}
